home *** CD-ROM | disk | FTP | other *** search
- Path: news.delphi.com!usenet
- From: JGUILLORY@delphi.com
- Newsgroups: comp.lang.c++
- Subject: passing struct to constructor - temp.txt [1/1]
- Date: 9 Mar 1996 17:18:25 GMT
- Organization: Delphi Internet Services Corporation
- Message-ID: <4hseh1$rkk@news2.delphi.com>
- References: <4eqgq3$5g1@geraldo.cc.utexas.edu>
- NNTP-Posting-Host: bos1g.delphi.com
- X-NewsReader: Rainbow V 1.20.0 for Delphi
-
-
- Quoting wharris from a message in comp.lang.c++
- wh>The compiler error I'm getting is "VIN is not a member of struct1". The
- wh>code has been severely minimized, however this should have the bare
- wh>essentials required to solve the problem.
- wh>class CarData
- wh>{
- wh>private :
- wh>char VIN[7];
- wh>public :
- wh>CarData(struct astruct1 CarInf);
- wh>};
- wh>//********************************************************
- wh>CarData::CarData(struct astruct1 CarInf)
- wh>{
- wh>strcpy(VIN,CarInf.VIN);
- wh>}
- wh>struct astruct1
- wh> {
- wh> char VIN[7];
- wh> }CarInfo;
- wh>void main()
- wh>{
- wh>CarData CarConst1(struct astruct1 CarInfo);
- wh>}
-
- The key is 'private:' Using private restricts access to VIN
- by any other part of your program except from within the object
- (CarData). Eg. you could create another function inside of CarData
- eg:
-
- void CarData::Test(void) {
- CarConst1(VIN);
- };
-
- But, that presents 2 problems, 1> Your constructor should have already
- been initialized by the time your program has a chance to call
- CarData::Test() .... 2> Why are you naming your constructor CarConst1?
- Constructors for objects should always be named the same as their
- object, and Destructors the same, except a ~ in front of the name.
- With that naming scheme, the constructor gets called as soon as you
- use new or allocate memory for the object. If you need more than
- one set of paramaters for the constructor, create as many constructors
- as you need, this technique is I believe called "Overloading")
- Eg:
-
- class Car {
- private:
- int data;
-
- public:
- Car(int a) { data = a; }
- Car() { data = 0; }
- };
-
-
- Thus:
-
- void main(void) {
- Car Mine(),Yours(2);
-
- }
-
- Mine.data would equal 0,
- Yours.data would equal 2.
-
- `[0;1;34mJohn H. Guillory
- `[33mLoving the Lord in '96 `[34mJGuillory@Delphi.Com
-
- I have read and understood the above X______________.
-
-
-